<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->

@section('content')
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>标题
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="L_title" name="title" value="{{ $data['title'] }}" required="" lay-verify="title" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>所属分类</label>
                <div class="layui-input-block">
                    <select name="cate_id" lay-verify="required">
                        @foreach($ad_cate as $item)
                        @if($item['id']==$data['cate_id'])
                        <option value="{{ $item['id'] }}" selected>{{ $item['name'] }}</option>
                        @else
                        <option value="{{ $item['id'] }}">{{ $item['name'] }}</option>
                        @endif
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">广告图片</label>
                <input type="text" id="image" name="image" value="{{ $data['image'] }}" required="" autocomplete="off" class="layui-input layui-input-inline" style="width: 189px;">
                <button type="button" class="layui-btn" id="image_btn">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">链接地址</label>
                <div class="layui-input-inline">
                    <input type="text" id="L_url" name="url" value="{{ $data['url'] }}" required="" lay-verify="url--" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="status" lay-verify="required">
                        @foreach($status as $key=>$item)
                        @if($key==$data['status'])
                        <option value="{{ $key }}" selected>{{ $item }}</option>
                        @else
                        <option value="{{ $key }}">{{ $item }}</option>
                        @endif
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">提交</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer',  'jquery'], function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(add)', function(data) {
            console.log(data);
            //发异步，把数据提交给php
            $.ajax({
                type: 'POST',
                url: "/admin/system/adEdit?id={{$data['id']}}",
                data: data.field,
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function(data) {
                    // console.log(data);
                    if (data.code == 1) {
                        layer.alert("操作成功", {
                                icon: 6
                            },
                            function() {
                                //关闭当前frame
                                xadmin.close();

                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                    } else {
                        layer.alert(data.msg)
                    }
                },
                error: function(xhr, type) {
                    alert('Ajax error!')
                }
            });

            return false;
        });

    });
    // 公司图片上传
    layui.use('upload', function() {
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#image_btn', //绑定元素
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            url: '/admin/index/imageUpload/', //上传接口
            done: function(res) {
                console.log(res);
                $("#image").attr('value', res.data.image);
                //上传完毕回调
            },
            error: function() {
                //请求异常回调
            }
        });
    });
    layui.use(['laydate'], function() {
        var laydate = layui.laydate;
        var form = layui.form;
        //执行一个laydate实例
        laydate.render({
            elem: '#opTime', //指定元素
            type: 'datetime'
        });

    });
</script>

@endsection
